/* 
 * File:   HostInfo.cpp
 * Author: Bram
 * 
 * Created on January 9, 2013, 2:42 PM
 */

#include "HostInfo.h"

HostInfo::HostInfo() {
    openHostDb();
}

HostInfo::HostInfo(const std::string& hostName, hostType type)
{
    searchHostDB = 0;
    if (type == NAME)
    {
        // Retrieve host by name
        hostPtr = gethostbyname(hostName.c_str());

        if (hostPtr == NULL)
        {

            dasLog << "error 001 getting host by name \r\n";
            cout << "error 001 getting host by name \r\n";
            exit(1);
        }
    }
    else if (type == ADDRESS)
    {
        // Retrieve host by address
        unsigned long netAddr = inet_addr(hostName.c_str());
        if (netAddr == -1)
        {
            dasLog << "Error 002 calling inet_addr() \r\n";
            cout << "Error 002 calling inet_addr() \r\n";
            exit(1);
        }

        //getnameinfo()
        hostPtr = gethostbyaddr((char *)&netAddr, sizeof(netAddr), AF_INET);
        if (hostPtr == NULL)
        {
            dasLog << "error 003 getting host by name! host name: " << hostName.c_str() << ", addr: " << netAddr << "\r\n";
            cout << "error 003 getting host by name! host name: " << hostName.c_str() << ", addr: " << netAddr << "\r\n";
            exit(1);
        }
    }
    else
    {
        dasLog << "Error 004 unknown host type: host name/address has to be given \r\n";
        cout << "Error 004 unknown host type: host name/address has to be given \r\n";
        exit(1);
    }
}

HostInfo::HostInfo(const HostInfo& orig) {
}

HostInfo::~HostInfo() {
    endhostent();
}

